import javax.swing.*;
import java.util.*;
import java.io.File;
import java.awt.Color;
import java.awt.event.*;

public abstract class Shape {
    protected int centerX, centerY;
    protected int size = 50;
    public abstract void draw(Graphics g);

    public Shape(int centerX, int centerY) {
        this.centerX = centerX;
        this.centerY = centerY;
    }
}

public interface Movable {
    public void moveUp(int amount);
    public void moveDown(int amount);
}

// C2开始
public class Circle extends Shape implements Movable {
    public Circle(int centerX, int centerY) {
        super(centerX, centerY);
    }
    @Override
    public void draw(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillOval(this.centerX,this.centerY,this.size,this.size); //fix:前两个参数不知道是左上角坐标还是中心坐标，这里先写中心坐标了
    }
    //C2结束
    //C4开始
    @Override
    public void moveUp(int amount) {
        this.centerY-=amount;
    }
    @Override
    public void moveDown(int amount) {
        this.centerY+=amount;
    }
    //C4结束
}

//C3开始
public class MyCanvas extends JPanel {
    public Shape s;
    public setShape(Shape s) {
        this.s=s;
    }
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        this.s.draw(g);
    }
}
//C3结束（局部）

public class sectionC {
    private static void createAndShowGUI() {
        //C1开始
        JFrame frame = new JFrame("有点意思嗷");
        frame.setSize(250, 250);
        JButton button1 = new JButton("^");
        JButton button2 = new JButton("v");
        button1.setBounds(0,0,250,25);
        button2.setBounds(240,0,250,25); //fix:这个坐标大概得改下
        //C1结束
        //C3开始
        Circle c=new Circle(125, 125);
        MyCanvas mc=new MyCanvas();
        mc.setShape(c);
        frame.add(mc);
        //C3结束
        //C5开始
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(mc.s instanceof Movable) {
                    Movable m=(Movable)mc.s;
                    m.moveUp(10);
                    mc.repaint();
                } 
            }
        });
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(mc.s instanceof Movable) {
                    Movable m=(Movable)mc.s;
                    m.moveDown(10);
                    mc.repaint();
                }
            }
        });
        frame.add(button1);
        frame.add(button2);
        //C5结束
        frame.setVisible(true); // 显示窗口
    }

    public static void main(String[] args) {
        // 显示应用 GUI
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}